package sfms.view;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import sfms.model.Facility;
import sfms.model.Model;
import sfms.model.Venue;
import sfms.util.CustomDate;

public class EditFacility extends javax.swing.JFrame {

    private static final long serialVersionUID = 1L;
    private Facility facility;
    private Model model;
    private DefaultTableModel tableModel;
    private sfms.controller.Staff controller;
    
    public EditFacility(java.awt.Frame parent, boolean modal, Model model, Facility facility, sfms.controller.Staff controller) {
        super("Edit Facility");
        initComponents();
        setLocationRelativeTo(null);
        this.model = model;
        this.facility = facility;
        this.controller = controller;
        this.setValues();
        
        tableModel = new DefaultTableModel(
            new Object[][] {}, new String [] {"ID", "Venue", "Capacity"}) {
                Class[] types = new Class[] {
                    Integer.class, String.class, Integer.class
                };

                @Override
                public Class getColumnClass(int columnIndex) {
                    return types[columnIndex];
                }
            };

        tableVenue.setModel(tableModel);
        this.controller.setModelVenue(tableModel);

        
        controller.refreshVenueTable(facility.getId());
 
        this.setVisible(true);
    }
    
    public void refreshTable(){
        controller.refreshVenueTable(facility.getId());
    }
    
    private void setValues(){
        
        this.textTitle.setText(facility.getTitle());
        if (facility.getLicense() == null){
            this.textLicense.setText("no");
        }
        else{
            this.textLicense.setText("yes");
        }
        
        if (facility.getNotActive() == null){
            this.setButtons(1);
        }
        else if (facility.getNotActive().compareTo(new java.util.Date()) < 0){
            this.setButtons(0);
        }
        else{
            this.setButtons(-1);
        }
        
        
        
    }
    
    
    /**
     * 
     * @param active 1 if active, 0 if disabled and -1 if disabling
     */
    private void setButtons(int active){
        if (active == 1){
            this.textStatus.setText("Active");
            this.buttonActive.setText("Deactivate in 7 days");
            this.buttonActive.setEnabled(true);
            this.buttonDeleteFacility.setEnabled(false);
        }
        else if (active == 0){
            this.textStatus.setText("Disabled");
            this.buttonActive.setText("Activate");
            this.buttonActive.setEnabled(true);
            this.buttonDeleteFacility.setEnabled(true);
            
        }
        else{
            CustomDate cdate = new CustomDate(facility.getNotActive());
            this.textStatus.setText("Disabled on "+cdate);
            this.buttonDeleteFacility.setEnabled(false);
            this.buttonActive.setText("Activate");
            this.buttonActive.setEnabled(false);
        }
        
        if (active == 0){
            this.buttonDeleteVenue.setEnabled(true);
            this.buttonAddVenue.setEnabled(true);
        }
        else{
            this.buttonDeleteVenue.setEnabled(false);
            this.buttonAddVenue.setEnabled(false);
        }
    }
    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        textTitle = new javax.swing.JLabel();
        textLicense = new javax.swing.JLabel();
        textStatus = new javax.swing.JLabel();
        buttonActive = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        buttonAddVenue = new javax.swing.JButton();
        buttonDeleteVenue = new javax.swing.JButton();
        buttonDeleteFacility = new javax.swing.JButton();
        buttonClose = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableVenue = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Facility");
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Edit Facility");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Facility Title:");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Requires License:");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Status:");

        textTitle.setText("textTitle");

        textLicense.setText("textLicense");

        textStatus.setText("textStatus");

        buttonActive.setText("Deactivate in 7 Days");
        buttonActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActiveActionPerformed(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setText("Venues:");

        buttonAddVenue.setText("Add Venue");
        buttonAddVenue.setEnabled(false);
        buttonAddVenue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddVenueActionPerformed(evt);
            }
        });

        buttonDeleteVenue.setText("Delete Venue");
        buttonDeleteVenue.setEnabled(false);
        buttonDeleteVenue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteVenueActionPerformed(evt);
            }
        });

        buttonDeleteFacility.setText("Delete Facility");
        buttonDeleteFacility.setEnabled(false);
        buttonDeleteFacility.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteFacilityActionPerformed(evt);
            }
        });

        buttonClose.setText("Close");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCloseActionPerformed(evt);
            }
        });

        tableVenue.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tableVenue);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(textLicense, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(textStatus, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(textTitle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(buttonDeleteVenue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(332, 332, 332)
                        .add(buttonAddVenue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(buttonClose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2))
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .add(196, 196, 196)
                .add(buttonActive)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(buttonDeleteFacility)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(textTitle))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(textLicense))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(textStatus))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(buttonActive)
                    .add(buttonDeleteFacility, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(0, 0, 0)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 175, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(buttonClose)
                    .add(buttonDeleteVenue)
                    .add(buttonAddVenue)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCloseActionPerformed

    private void buttonActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonActiveActionPerformed

        if (JOptionPane.showConfirmDialog(this, "Are you sure?", "Are you sure?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
            if (this.buttonActive.getText().equals("Activate")){
                facility = model.enableFacility(facility, true);
                this.setButtons(1);
            }
            else{
                facility = model.enableFacility(facility, false);
                this.setButtons(-1);
            }
        }
    }//GEN-LAST:event_buttonActiveActionPerformed

    private void buttonAddVenueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddVenueActionPerformed
        CreateVenue cv = new CreateVenue(this, true, model, facility);
    }//GEN-LAST:event_buttonAddVenueActionPerformed

    private void buttonDeleteFacilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteFacilityActionPerformed
        try{
        if (JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this facility?") == JOptionPane.OK_OPTION){
            model.removeFacility(facility.getId());
            this.setVisible(false);
            this.dispose();
            
        }
        }catch(Exception e){}
    }//GEN-LAST:event_buttonDeleteFacilityActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       
            controller.refreshFacilityTable();
    }//GEN-LAST:event_formWindowClosed

    private void buttonDeleteVenueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteVenueActionPerformed
        // TODO add your handling code here:
        
        try{
        if (JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this venue?") == JOptionPane.OK_OPTION){
            int selected = tableVenue.getSelectedRow();
            if (selected != -1){
                controller.deleteVenue((Integer)tableModel.getValueAt(selected, 0));
                refreshTable();
            }
        }
        }catch(Exception e){}
    }//GEN-LAST:event_buttonDeleteVenueActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonActive;
    private javax.swing.JButton buttonAddVenue;
    private javax.swing.JButton buttonClose;
    private javax.swing.JButton buttonDeleteFacility;
    private javax.swing.JButton buttonDeleteVenue;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tableVenue;
    private javax.swing.JLabel textLicense;
    private javax.swing.JLabel textStatus;
    private javax.swing.JLabel textTitle;
    // End of variables declaration//GEN-END:variables
}
